<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\Admin;
use Illuminate\Http\Request;

class AdminController extends BaseController
{

    public function index(Request $request) {
        $title = $request->input('title');
        $users = Admin::when($title, function ($q)use($title) {
            $q->where('username', 'like', '%'. $title . '%');
        })->paginate(\request('per_page', 20));
        return $this->paginate($users);
    }

    public function store(Request $request) {
        $input = $request->input();
        $this->validate($request, [
            'id' => 'filled|exists:admins',
            'role' => 'required|in:1,2,3',
            'username'  => 'required|min:4|max:14',
        ]);
        $id = \request("id");

        $is_update_pwd = false;
        if (!$id) {
            if (empty($input['password']))
                $input['password'] = 'abc123456';
            $input['password'] = bcrypt($input['password']);
        } else {
            if (empty($input['password'])) {
                unset($input['password']);
            } else {
                $input['password'] = bcrypt($input['password']);
                $is_update_pwd = true;
            }
        }

        $exist_id = Admin::where('username', \request('username'))->value('id');
        if ($id) {
            if ($exist_id && ($id != $exist_id)) {
                return $this->failed("用户名已经存在");
            }
            $admin = Admin::where('id', $id);
            $admin->update($input);
            if ($is_update_pwd) {
                $token = \Cache::get("token:" .  $id);
                if ($token)
                    auth()->setToken($token)->invalidate(true);
            }
        } else {
            if ($exist_id)
                return $this->failed("用户名已经存在");

            $admin = Admin::create($input);
            $id = $admin->id;
        }
        return $this->success(['id' => $id]);
    }

    /**
     * 详情
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function info(Request $request) {
        $this->validate($request, [
            'id' => 'required|exists:admins'
        ]);
        $id = \request("id");
        $info = Admin::find($id);
        return $this->success($info);
    }

    public function updatePwd(Request $request) {
        $input = $request->input();
        $this->validate($request, [
            'password'  => 'required|min:6|max:14',
            'password_confirmation' => 'required|same:password'
        ]);
        $user = $this->getUser();
        $user->password = bcrypt($input['password']);
        if ($user->save()){
            auth()->refresh();
            $token = auth()->login($user);
            return $this->respondWithToken($token);
        }
        return $this->failed('创建失败');
    }

    public function destroy(Request $request) {
        $this->validate($request, [
            'id' => 'required|exists:admins'
        ]);
        $id = $request->input('id');
        Admin::where('id', $id)->delete();
        return $this->success([]);
    }

    public function logout(Request $request) {
        auth()->logout();
        return $this->success([]);
    }

    public function resetPassword(Request $request) {
        $this->validate($request, [
            'id'   => 'required',
        ]);
        $id = \request('id');
        $admin = Admin::find($id);
        if (!$admin)
            return $this->failed('ID无效');
        $admin->password = bcrypt('abc123456');
        if ($admin->save()) {
            $token = \Cache::get("token:" .  $id);
            if ($token)
                auth()->setToken($token)->invalidate(true);
        }
        return $this->success([], '重置成功');
    }


}

